package net.gcode.sdkscheduleparser.core.write;

import java.rmi.server.UID;
import java.util.Iterator;
import java.util.Vector;

import net.gcode.sdkscheduleparser.core.Modul;
import net.gcode.sdkscheduleparser.core.Stdplan;
import net.gcode.sdkscheduleparser.core.Vorlesung;
import net.gcode.sdkscheduleparser.intface.ScheduleExport;

public class Export_vcal implements ScheduleExport {

	@Override
	public String ExportConverter(Stdplan stundenplan, boolean utc) {
		String ret = "BEGIN:VCALENDAR\nPRODID:-//Arcsoft Corporation//MMS Composer//EN\nVERSION:2.0\nCALSCALE:GREGORIAN\nMETHOD:PUBLISH\n";
		Iterator<Vorlesung> it = stundenplan.getVorlesung().iterator();
		while (it.hasNext()) {
			Vorlesung tmp = it.next();
			ret += buildVorlesung(utc, tmp);
		}
		ret += "END:VCALENDAR";
		return ret;
	}

	public String ExportConverter(Vector<Modul> module, boolean utc) {
		String ret = "BEGIN:VCALENDAR\nPRODID:-//Arcsoft Corporation//MMS Composer//EN\nVERSION:2.0\nCALSCALE:GREGORIAN\nMETHOD:PUBLISH\n";
		for (Modul modul : module) {
			for (Vorlesung vorlesung : modul.getVorlesungen()) {
				ret += buildVorlesung(utc, vorlesung);
			}
		}
		ret += "END:VCALENDAR";
		return ret;
	}

	private String buildVorlesung(boolean utc, Vorlesung tmp) {
		String ret = "";
		ret += "BEGIN:VEVENT\n";
		UID id = new UID();
		ret += "UID:" + id.toString() + "\n";
		ret += "SUMMARY:" + tmp.getBez().replace(",", "\\,") + "\n";
		ret += "CLASS:PUBLIC\n";
		int tag = tmp.getTag();
		int mon = tmp.getMonat();
		int zeit = tmp.getAnfzeit();
		if (utc) {
			zeit = toUtc(zeit);
		}

		ret += "DTSTART:" + tmp.getJahr() + (mon < 10 ? "0" + mon : mon)
				+ (tag < 10 ? "0" + tag : tag) + "T" + (zeit < 1000 ? "0" + zeit : zeit) + "00z\n";

		zeit = tmp.getEndzeit();
		if (utc) {
			zeit = toUtc(zeit);
		}
		ret += "DTEND:" + tmp.getJahr() + (mon < 10 ? "0" + mon : mon)
				+ (tag < 10 ? "0" + tag : tag) + "T" + (zeit < 1000 ? "0" + zeit : zeit) + "00z\n";
		if (tmp.getProf() != null) {
			ret += "DESCRIPTION:" + tmp.getProf().replace(",", "\\,") + "\n";
		}
		if (tmp.getKurs() != null) {
			ret = ret + "\\n" + tmp.getKurs().replace(",", "\\,") + "\n";
		}
		if (tmp.getRaum() != null) {
			ret += "LOCATION:Raum " + tmp.getRaum().replace(",", "\\,") + "\n";
		}
		ret += "DTSTAMP:20090910T101617Z\n";
		ret += "CATEGORIES:Vorlesung\n";
		ret += "SEQUENCE:0\nPRIORITY:5\n";
		ret += "END:VEVENT\n";
		return ret;
	}

	private static int toUtc(int zeit) {

		String timeString = new Integer(zeit).toString();
		String minutes = timeString.substring(timeString.length() - 2);
		Integer hours = new Integer(timeString.substring(0, timeString.length() - 2));

		// Outlook expects UTC Time coordinates.
		// hours--;

		Integer result = new Integer(hours.toString() + minutes.toString());
		return result;
	}

	@Override
	public String getExtension() {

		return "vcs";
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "VCalender";
	}

}
